package com.sinba.common.system.action;

import com.opensymphony.xwork2.ActionSupport;
import com.sinba.common.utils.AppConfigUtils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;


/**
 * 文件上传Action类.
 * @author Van.
 * 修改日期：2011-02-10
 */
public class FileUploadAction
    extends ActionSupport
{
    private static final long serialVersionUID = 572146812454L;
    private static final int BUFFER_SIZE = 64 * 1024;
    private File myFile;
    private String fileName;
    private String imageFileName;
    private String id;
    private String myFileName;
    private static final String IMAGEPATH="";
    private static final String JASPERPATH="";
    private static final String REQUESTPATH="";
    private static final String ATTAPATH=AppConfigUtils.getInstance().getPhysicPath("");
    private String imgSize;
    private String imageFileNametoreuqest;
    private String path;
    
    
    public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public String getImageFileNametoreuqest() {
		return imageFileNametoreuqest;
	}
	public void setImageFileNametoreuqest(String imageFileNametoreuqest) {
		this.imageFileNametoreuqest = imageFileNametoreuqest;
	}
	public String getImgSize() {
		return imgSize;
	}
	public void setImgSize(String imgSize) {
		this.imgSize = imgSize;
	}
	public String getMyFileName() {
		return myFileName;
	}
	public void setMyFileName(String myFileName) {
		this.myFileName = myFileName;
	}
	public String getId(){
        return id;
    }
    public void setId( String id ){
        this.id = id;
    }
    public void setMyFileFileName( String fileName ){
        this.fileName = fileName;
    }
    public void setMyFile( File myFile ){
        this.myFile = myFile;
    }
    public String getImageFileName(){
        return imageFileName;
    }
    
    

    public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}
	/**
     * 复制（上传）文件.
     * @param File 输入文件
     * @param File 输出文件
     */
    private static void copy( File src, File dst )
    {
        try
        {
        	
        	
        	if(dst.exists()){
        		dst.delete();
        	}
            InputStream in = null;
            OutputStream out = null;
            try{
                in = new BufferedInputStream( new FileInputStream( src ),BUFFER_SIZE );
                out = new BufferedOutputStream( new FileOutputStream( dst ),BUFFER_SIZE );
                byte[] buffer = new byte[BUFFER_SIZE];

                while ( in.read( buffer ) > 0 ){
                    out.write( buffer );
                }
            } finally{
                if ( null != in ){
                    in.close();
                }
                if ( null != out ){
                    out.close(  );
                }
            }
        } catch ( Exception e ){
            e.printStackTrace();
        }
    }
    public void fileUpload(){
    	fileUpload(myFile,fileName);
    }
    public void fileUpload(File file,String fileName){
    	File outputJrxmlFile=null;
    	
        if("banner".equals(path)){
        	outputJrxmlFile=new File(ATTAPATH+"/images/banner.jpg");
        }else{
        	outputJrxmlFile=new File(ATTAPATH+"/upload/"+imageFileNametoreuqest);
        }
    	
        copy(file,outputJrxmlFile);
	}
    /**
     * 获取文件类型.
     * @param String 文件名
     * @return String 文件类型
     */
    private static String getExtention( String fileName ){
        int pos = fileName.lastIndexOf( "." );
        return fileName.substring( pos );
    }
    /**
     * 执行上传操作.
     */
    @Override
    public String execute(){
    	
    
    	try{

	        if(imgSize!=null){
		        if(myFile.length()>150000){
	        		imageFileName="Image is too big";
	        		return "ERROR";
	        	}
	        }
	        String fileUrl="";
	      
	        if(imageFileName!=null){
	        	fileUrl = JASPERPATH+"/"+imageFileName;
	        	
	        	myFile=new File(IMAGEPATH+"/"+myFileName);
	        	
	        }else{
	        	fileUrl = IMAGEPATH + "/"+new Date().getTime() + getExtention(fileName );
	        	imageFileName = new Date().getTime() + getExtention( fileName );
	        }
	        File imageFile =new File( fileUrl );
	        copy( myFile, imageFile );
	        return SUCCESS;
    	}
        catch(Exception ex){
        	
        	ex.printStackTrace();
        	
        	return "";
        	
        }
    }
}
